const { Token } = require("../tools/baseCode");
const NodeCache = require("node-cache");
const { Register } = require("../schema");
const { successMessage, failMessage, errMessage } = require("../tools/message");

const Cache = new NodeCache({ stdTTL: 1800, checkperiod: 2400 });
/**
 * 登录校验
 */
exports.login = (Router) => {
	Router.post("/login", (req, res) => {
		Register.findOne({ root: req.body.root })
			.then((success) => {
				if (req.body.root === success.root && req.body.pwd === success.pwd) {
					// 缓存双重认证
					Cache.set(req.body.root, `Bearer ${Token}`);
					Cache.set(req.body.root + "_host", req.headers.host);
					res.setHeader("Authorization", `Bearer ${Token}`);
					successMessage(req, res, "登录");
				} else {
					failMessage(req, res, "帐号或密码错误");
				}
			})
			.catch((err) => {
				errMessage(req, res, err);
			});
		// 缓存
		// 返回
	});
};
